//
//  MqForumCommonViewController.m
//  Meiqi
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "MqForumCommonViewController.h"
#import "MqForumModuleDetailViewController.h"
@interface MqForumCommonViewController ()
<UITableViewDataSource, UITableViewDelegate>
@end

@implementation MqForumCommonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:(UITableViewStylePlain)];
    
    //  设置代理
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    //  表头
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
    UILabel *exploreLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 80, 30)];
    exploreLabel.text = @"经常浏览";
    [view addSubview:exploreLabel];
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(kScreenWidth - 120, exploreLabel.top, 120, 30);
    [button setTitle:@"清除浏览历史" forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(clearNotes:) forControlEvents:(UIControlEventTouchUpInside)];
    [button setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
    [view addSubview:button];
    
    _tableView.tableHeaderView = view;
    
    //  注册cell
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
}

//  清除浏览记录
- (void)clearNotes:(UIButton *)button{
    
    [[[MqManagerHandle sharedManager] historyArray] removeAllObjects];
    [_tableView reloadData];
    
}

#pragma mark --- tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [MqManagerHandle sharedManager].historyArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    //  取出存在单例类数组中对应的记录
    MqManagerHandle *handle = [MqManagerHandle sharedManager];
    MqForumModule *module = handle.historyArray[indexPath.row];
    cell.textLabel.text = module.title;
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MqManagerHandle *handle = [MqManagerHandle sharedManager];
    MqForumModule *module = handle.historyArray[indexPath.row];
    MqForumModuleDetailViewController *detailVC = [[MqForumModuleDetailViewController alloc] init];
    
    detailVC.fid = module.fid;
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
